package com.example.demo6;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.demo6.SQLite.User;

public class MyUserHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "UserManager.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_USER = "User";
    private static final String COLUMN_USERID = "userid";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";
    private static final String COLUMN_PHONE = "phone";
    private static final String COLUMN_REGION = "region";

    public MyUserHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = "CREATE TABLE " + TABLE_USER + "("
                + COLUMN_USERID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_USERNAME + " TEXT NOT NULL UNIQUE,"
                + COLUMN_PASSWORD + " TEXT NOT NULL,"
                + COLUMN_PHONE + " INTEGER NOT NULL,"
                + COLUMN_REGION + " TEXT NOT NULL" + ")";
        db.execSQL(createTableQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Handle database upgrades if needed
//        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        String dropTable = "DROP TABLE IF EXISTS " + TABLE_USER;
        db.execSQL(dropTable);
        onCreate(db);
    }

    // 插入用户数据
    public long insertUser(User user) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_USERNAME, user.getUsername());
        contentValues.put(COLUMN_PASSWORD, user.getPassword());
        contentValues.put(COLUMN_PHONE, user.getPhone());
        contentValues.put(COLUMN_REGION, user.getRegion());
        return db.insert(TABLE_USER, null, contentValues);
    }

    // 更新用户数据，现在使用userid作为条件
    public int updateUser(User updatedUser) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_USERNAME, updatedUser.getUsername());
        contentValues.put(COLUMN_PASSWORD, updatedUser.getPassword());
        contentValues.put(COLUMN_PHONE, updatedUser.getPhone());
        contentValues.put(COLUMN_REGION, updatedUser.getRegion());

        String selection = COLUMN_USERID + " = ?";
        String[] selectionArgs = {Integer.toString(updatedUser.getUserid())};
        return db.update(TABLE_USER, contentValues, selection, selectionArgs);
    }

    // 删除用户数据，通过userid删除
    public int deleteUser(int userId) {
        SQLiteDatabase db = this.getWritableDatabase();
        String selection = COLUMN_USERID + " = ?";
        String[] selectionArgs = {Integer.toString(userId)};
        return db.delete(TABLE_USER, selection, selectionArgs);
    }

    // 查询单个用户数据，通过userid查询
    public User getUserById(int userId) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_USER, null, COLUMN_USERID + "=?", new String[]{Integer.toString(userId)}, null, null, null);
        User user = null;
        if (cursor != null && cursor.moveToFirst()) {
            user = new User(
                    cursor.getInt(cursor.getColumnIndex(COLUMN_USERID)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD)),
                    cursor.getInt(cursor.getColumnIndex(COLUMN_PHONE)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_REGION))
            );
            cursor.close();
        }
        return user;
    }

    // 查询所有用户数据
    public Cursor getAllUsers() {
        SQLiteDatabase db = this.getReadableDatabase();
        return db.rawQuery("SELECT * FROM " + TABLE_USER, null);
    }
}